﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHXH.Core.IService;
using FX.Core;
using CRM.Admin;
using BHXH.Core.Domain;
using NHibernate.Cfg;
using NHibernate;

namespace BHXH.Frm.FrmUserControl
{
    public partial class D01b_TS : UserControl
    {
        private int ID = 4;
        private string IDRef = "4";
        hs_D01bTS_ms hoso_congty = new hs_D01bTS_ms();
        private readonly Ihs_D01bTS_msService thongtin_hoso_bhSrv;       

        public D01b_TS()
        {
            InitializeComponent();
            thongtin_hoso_bhSrv = IoC.Resolve<Ihs_D01bTS_msService>();
        }

        public bool Check_DuLieuDauVao(string socongvan, string bhxh, string tendonvi, string masoquanly, string diachi, string hosoguikem, string noidung, string lydo)
        {
            if (socongvan == "" || bhxh == "" || tendonvi == "" || masoquanly == "" || diachi == "" || hosoguikem == "" || noidung == "" || lydo == "")
                return false;
            else if (socongvan.Length > 256 || bhxh.Length > 256 || tendonvi.Length > 256 || masoquanly.Length > 256 || diachi.Length > 256 || hosoguikem.Length > 256)
                return false;
            return true;
        }

        private void D01b_TS_Load(object sender, EventArgs e)
        {
            Load_Data(ID, IDRef);
        }

        public void Load_Data(int ID, string IDref)
        {
            hoso_congty = thongtin_hoso_bhSrv.Load_ThongTinHoSo(ID, IDref);

            lblCheckID.Text = hoso_congty.ID.ToString();

            this.txtSoCongVan.Text = hoso_congty.So;
            this.txtBHXH.Text = hoso_congty.KinhGui;
            this.txtTenDonVi.Text = hoso_congty.TenDV;
            this.txtMaSoQuanLi.Text = hoso_congty.MSTQL;
            this.txtDiaChi.Text = hoso_congty.DiaChi;
            this.txtNoiDung.Text = hoso_congty.NoiDung;
            this.txtLyDo.Text = hoso_congty.LyDo;
            this.txtHoSoGuiKem.Text = hoso_congty.HSGK;
        }


        public void UpDate_Save_DangKi_BaoHiem()
        {
            hoso_congty.IDRef = IDRef;
            hoso_congty.So = this.txtSoCongVan.Text.ToString();
            hoso_congty.KinhGui = this.txtBHXH.Text.ToString();
            hoso_congty.TenDV = this.txtTenDonVi.Text.ToString();
            hoso_congty.MSTQL = this.txtMaSoQuanLi.Text.ToString();
            hoso_congty.DiaChi = this.txtDiaChi.Text.ToString();
            hoso_congty.NoiDung = this.txtNoiDung.Text.ToString();
            hoso_congty.LyDo = this.txtLyDo.Text.ToString();
            hoso_congty.HSGK = this.txtHoSoGuiKem.Text.ToString();

            if (lblCheckID.Text != "0")
            {
                hoso_congty.ID = int.Parse(lblCheckID.Text);
                thongtin_hoso_bhSrv.UpDate_ThongTinHoSo(hoso_congty);
            }
            else
            {
                thongtin_hoso_bhSrv.Add_ThongTinHoSo(hoso_congty);
            }

        }

        public void Ghi_D01B_TS()
        {
            if (!Check_DuLieuDauVao(this.txtSoCongVan.Text.ToString(), this.txtBHXH.Text.ToString(), this.txtTenDonVi.Text.ToString(), this.txtMaSoQuanLi.Text.ToString(), this.txtDiaChi.Text.ToString(), this.txtHoSoGuiKem.Text.ToString(), this.txtNoiDung.Text.ToString(), this.txtLyDo.Text.ToString()))
            {
                MessageBox.Show(MainResource.msgDuLieuTrong, MainResource.textCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                UpDate_Save_DangKi_BaoHiem();
            }
        }

        public void NhapLai_D01B_TS()
        {
            Load_Data(ID, IDRef);
        }
    }
}
